added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSGDIPlusManipulateImage / MainForm.cs
blobb1cdfac77abe7cdc99d06dc7b51b15ec429709bc
1 /****************************** Module Header ******************************\
2 * Module Name: MainForm.cs
3 * Project: CSGDIPlusManipulateImage
4 * Copyright (c) Microsoft Corporation.
5 *
6 * This is the main form of this application. It is used to initialize the UI and
7 * handle the events.
8 *
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
13 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 \***************************************************************************/
18 using System;
19 using System.Drawing;
20 using System.Windows.Forms;
21 using System.Drawing.Drawing2D;
23 namespace CSGDIPlusManipulateImage
25 public partial class MainForm : Form
27 Pen pen = null;
29 ImageManipulator imgManipulator = null;
31 Point adjustment = Point.Empty;
33 public MainForm()
35 InitializeComponent();
37 // Load a bitmap from a local file.
38 Bitmap img = new Bitmap("OneCodeIcon.png");
40 // Initialize the bmpEx.
41 imgManipulator = new ImageManipulator(img);
43 // Add all the InterpolationMode to the combobox.
44 for (int i = 0; i <= 7; i++)
46 cmbInterpolationMode.Items.Add((InterpolationMode)(i));
49 cmbInterpolationMode.SelectedIndex = 0;
53 /// <summary>
54 /// Handle the click event of the buttons btnRotateLeft, btnRotateRight,
55 /// btnFlipVertical and btnFlipHorizontal.
56 /// </summary>
57 private void btnRotateFlip_Click(object sender, EventArgs e)
59 Button rotateFlipButton = sender as Button;
61 if (rotateFlipButton == null)
63 return;
66 RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone;
68 switch (rotateFlipButton.Name)
70 case "btnRotateLeft":
71 rotateFlipType = RotateFlipType.Rotate270FlipNone;
72 break;
73 case "btnRotateRight":
74 rotateFlipType = RotateFlipType.Rotate90FlipNone;
75 break;
76 case "btnFlipVertical":
77 rotateFlipType = RotateFlipType.RotateNoneFlipY;
78 break;
79 case "btnFlipHorizontal":
80 rotateFlipType = RotateFlipType.RotateNoneFlipX;
81 break;
84 // Rotate or flip the image.
85 imgManipulator.RotateFlip(rotateFlipType);
87 // Redraw the pnlImage.
88 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
91 /// <summary>
92 /// Handle the click event of the button btnRotateAngle.
93 /// </summary>
94 private void btnRotateAngle_Click(object sender, EventArgs e)
96 float angle = 0;
98 // Verify the input value.
99 float.TryParse(tbRotateAngle.Text, out angle);
101 if (angle > 0 && angle < 360)
103 // Rotate or flip the image.
104 imgManipulator.RotateImg(angle);
106 // Redraw the pnlImage.
107 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
112 /// <summary>
113 /// Handle the click event of the buttons btnMoveUp, btnMoveDown,
114 /// btnMoveLeft and btnMoveRight.
115 /// </summary>
116 private void btnMove_Click(object sender, EventArgs e)
118 Button moveButton = sender as Button;
119 if (moveButton == null)
121 return;
124 int x = 0;
125 int y = 0;
127 switch (moveButton.Name)
129 case "btnMoveUp":
130 y = -10;
131 break;
132 case "btnMoveDown":
133 y = 10;
134 break;
135 case "btnMoveLeft":
136 x = -10;
137 break;
138 case "btnMoveRight":
139 x = 10;
140 break;
142 adjustment = new Point(adjustment.X + x, adjustment.Y + y);
144 // Redraw the pnlImage.
145 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
149 /// <summary>
150 /// Draw the image on the pnlImage when it is painted.
151 /// </summary>
152 private void pnlImage_Paint(object sender, PaintEventArgs e)
155 // Draw the pnlImage for the first time..
156 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
160 /// <summary>
161 /// Handle the click event of the buttons btnAmplify and btnMicrify.
162 /// </summary>
163 private void btnAmplify_Click(object sender, EventArgs e)
165 Button btnScale = sender as Button;
166 if (btnScale.Name == "btnAmplify")
168 imgManipulator.Scale(2, 2, (InterpolationMode)cmbInterpolationMode.SelectedItem);
170 else
172 imgManipulator.Scale(0.5f, 0.5f, (InterpolationMode)cmbInterpolationMode.SelectedItem);
175 // Redraw the pnlImage.
176 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
180 /// <summary>
181 /// andle the click event of the buttons btnSkewLeft and btnSkewRight.
182 /// </summary>
183 /// <param name="sender"></param>
184 /// <param name="e"></param>
185 private void btnSkew_Click(object sender, EventArgs e)
187 Button btnSkew = sender as Button;
189 switch(btnSkew.Name)
191 case "btnSkewLeft":
192 imgManipulator.Skew(-10);
193 break;
194 case "btnSkewRight":
195 imgManipulator.Skew( 10);
196 break;
199 // Redraw the pnlImage.
200 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
204 /// <summary>
205 /// Reset the bmpEx.
206 /// </summary>
207 private void btnReset_Click(object sender, EventArgs e)
210 // Dispose the bmpEx.
211 imgManipulator.Dispose();
213 // Load a bitmap from a local file.
214 Bitmap img = new Bitmap("OneCodeIcon.png");
216 // Initialize the bmpEx.
217 imgManipulator = new ImageManipulator(img);
219 // Redraw the pnlImage.
220 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);
224 /// <summary>
225 /// Handle the CheckedChanged event of the checkbox chkDrawBounds.
226 /// </summary>
227 private void chkDrawBounds_CheckedChanged(object sender, EventArgs e)
230 // If the pen is not null, draw the bounds of the image.
231 if (chkDrawBounds.Checked)
233 pen = Pens.Blue;
235 else
237 pen = null;
240 // Redraw the pnlImage.
241 imgManipulator.DrawControl(this.pnlImage, adjustment, pen);